package com.zucc.eye.controller;
import com.zucc.eye.entity.GitPageRepo;
import com.zucc.eye.repository.GitPageRepoRepository;
import com.zucc.eye.util.GitPageUtil;
import com.zucc.eye.util.ReturnT;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @description git_page_repo
 * @author zhengkai.blog.csdn.net
 * @date 2022-02-20
 */

@Slf4j
@Api(tags = "gitPageRepo")
@CrossOrigin
@RestController
@RequestMapping("/gitPageRepo")
public class GitPageRepoController {

    @Autowired
    private GitPageRepoRepository gitPageRepoRepository;

    /**
    * 新增或编辑
    */
    @PostMapping("/save")
    @ApiOperation(value = "save GitPageRepo", notes = "save GitPageRepo")
    public Object save(@RequestBody GitPageRepo gitPageRepo){
        try {
            return ReturnT.success(gitPageRepoRepository.save(gitPageRepo));
        } catch (Exception e) {
            e.printStackTrace();
            return ReturnT.error("保存失败");
        }

    }

    @PostMapping("/getRepos")
    @ApiOperation(value = "save GitPageRepo", notes = "save GitPageRepo")
    public Object getRepos(@RequestBody GitPageRepo gitPageRepo){

        try {
            List<GitPageRepo> allByPageAndQueryWord
                    = gitPageRepoRepository.findAllByPageAndQueryWord(gitPageRepo.getPage(), gitPageRepo.getQueryWord());
            if(allByPageAndQueryWord.size()==0){
                String queryWord = gitPageRepo.getQueryWord();

                List<GitPageRepo> githubPageRepos = GitPageUtil.getGithubPageRepos(queryWord,gitPageRepo.getPage());
//            return ReturnT.success(gitPageRepoRepository.save(gitPageRepo));
                List<GitPageRepo> list = gitPageRepoRepository.saveAll(githubPageRepos);
                return ReturnT.success(githubPageRepos);
            }

            return ReturnT.success(allByPageAndQueryWord);
        } catch (Exception e) {
            e.printStackTrace();
            return ReturnT.error("保存失败 "+e.getMessage());
        }

    }

    @PostMapping("/saveRepos")
    @ApiOperation(value = "save GitPageRepo", notes = "save GitPageRepo")
    public Object saveRepos(@RequestBody GitPageRepo gitPageRepo){

        try {
            String queryWord = gitPageRepo.getQueryWord();

            List<GitPageRepo> githubPageRepos = GitPageUtil.getGithubPageRepos(queryWord,gitPageRepo.getPage());
//            return ReturnT.success(gitPageRepoRepository.save(gitPageRepo));
            List<GitPageRepo> list = gitPageRepoRepository.saveAll(githubPageRepos);
            return ReturnT.success("ok");
        } catch (Exception e) {
            e.printStackTrace();
            return ReturnT.error("保存失败 "+e.getMessage());
        }

    }

    /**
    * 删除
    */
    @PostMapping("/delete")
    @ApiOperation(value = "delete GitPageRepo", notes = "delete GitPageRepo")
    public Object delete(int id){
        Optional<GitPageRepo> gitPageRepo=gitPageRepoRepository.findById(id);
        if(gitPageRepo.isPresent()){
            gitPageRepoRepository.deleteById(id);
            return ReturnT.success("删除成功");
        }else{
            return ReturnT.error("没有找到该对象");
        }
    }

    /**
    * 查询
    */
    @PostMapping("/find")
    @ApiOperation(value = "find GitPageRepo by id", notes = "find GitPageRepo by id")
    public Object find(int id){
        Optional<GitPageRepo> gitPageRepo=gitPageRepoRepository.findById(id);
        if(gitPageRepo.isPresent()){
            return ReturnT.success(gitPageRepo.get());
        }else{
            return ReturnT.error("没有找到该对象");
        }
    }

    /**
    * 分页查询
    */
    @PostMapping("/list")
    @ApiOperation(value = "list GitPageRepo", notes = "list GitPageRepo")
    public Object list(@RequestBody GitPageRepo gitPageRepo,
                        @RequestParam(required = false, defaultValue = "0") int pageNumber,
                        @RequestParam(required = false, defaultValue = "10") int pageSize) {

            try {
                //创建匹配器，需要查询条件请修改此处代码
                ExampleMatcher matcher = ExampleMatcher.matchingAll();

                //创建实例
                Example<GitPageRepo> example = Example.of(gitPageRepo, matcher);
                //分页构造
                Pageable pageable = PageRequest.of(pageNumber,pageSize);

                return ReturnT.success(gitPageRepoRepository.findAll(example, pageable));

            } catch (Exception e) {
                e.printStackTrace();
                return ReturnT.error(e.getMessage());
            }

    }

}
